/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NetworkAdapterView.java
 *
 * Created on 10.Şub.2010, 15:35:02
 */
package org.testis.gui.mvc.view.testbed;

import org.testis.gui.mvc.controller.TestBedControllerList;
import org.testis.gui.mvc.controller.testbed.NetworkAdapterController;
import org.testis.gui.mvc.utilities.Validatable;
import org.testis.gui.mvc.view.AbstractViewPanel;
import org.testis.gui.mvc.view.AtofBaseDialog;
import org.testis.orm.testbed.refined.NetworkAdapterRefined;
import org.testis.orm.testbed.refined.NetworkAdapterTypeFactory;
import org.testis.orm.testbed.refined.NetworkAdapterTypeRefined;
import org.testis.orm.testbed.refined.TestPcFactory;
import org.testis.orm.testbed.refined.TestPcRefined;
import java.awt.Frame;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author selcuk
 */
public class NetworkAdapterView extends AtofBaseDialog implements
        AbstractViewPanel, Validatable {

    private NetworkAdapterController m_controller;
    private boolean m_editMode;
    private int m_adapterId;
    private NetworkAdapterRefined m_adapterRefined;

    /** Creates new form NetworkAdapterView */
    public NetworkAdapterView(Frame owner, boolean modal, boolean editMode) {
        super(owner, modal);
        m_controller = TestBedControllerList.newInstance().getNetworkAdapterController();
        m_editMode = editMode;
        initComponents();
        setFrameIcon("images/testbed/" + NetworkAdapterRefined.TYPE_NAME.toLowerCase(
                Locale.ENGLISH) + ".png");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pcComboBox = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        adapterTypeComboBox = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        wpaDriverTextField = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(getWindowTitle());
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        pcComboBox.setModel(new DefaultComboBoxModel());
        populatePcComboBox();

        jLabel3.setText("Pc");

        jLabel6.setText("Name ");

        jLabel4.setText("Type");

        adapterTypeComboBox.setModel(new DefaultComboBoxModel());
        populateAdapterTypeComboBox();

        jLabel5.setText("WPA Driver");

        saveButton.setText("Save");
        setDefaultButton(saveButton);
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(adapterTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(wpaDriverTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                            .addComponent(pcComboBox, 0, 240, Short.MAX_VALUE)
                            .addComponent(nameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(closeButton)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {closeButton, saveButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel6)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel3)
                    .addComponent(pcComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel4)
                    .addComponent(adapterTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel5)
                    .addComponent(wpaDriverTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(closeButton)
                    .addComponent(saveButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        WindowListener[] listeners = getWindowListeners();
        for (int i = 0; i < listeners.length; i++) {
            this.removeWindowListener(listeners[i]);
        }
        this.setVisible(false);
        this.setEnabled(false);
        m_controller.operationCloseWindow();
        this.dispose();
    }//GEN-LAST:event_formWindowClosed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        if (!validateForm()) {
            return;
        }
        saveButton.setEnabled(false);

        String name = nameTextField.getText();
        int pcId = getSelectedTestPcId();
        int typeId = getSelectedAdapterTypeId();
        String wpaDriver = wpaDriverTextField.getText();

        if (m_editMode) {
            HashMap values = new HashMap();
            values.put("adapterId", m_adapterId);
            values.put("name", name);
            values.put("typeId", typeId);
            values.put("pcId", pcId);
            values.put("wpaDriver", wpaDriver);
            m_controller.operationSaveAdapter(m_adapterRefined, values);
        } else {
            m_controller.operationAddAdapter(name, typeId, pcId, wpaDriver);
        }
        dispose();
    }//GEN-LAST:event_saveButtonActionPerformed

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        dispose();
    }//GEN-LAST:event_closeButtonActionPerformed

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(
                NetworkAdapterController.VISIBLE_PROPERTY)) {
            this.centerParent();
        } else if (evt.getPropertyName().equals(
                NetworkAdapterController.ADAPTER_REFINED_PROPERTY)) {
            NetworkAdapterRefined val = (NetworkAdapterRefined) evt.getNewValue();
            m_adapterRefined = val;
        } else if (evt.getPropertyName().equals(
                NetworkAdapterController.ADAPTER_ID_PROPERTY)) {
            Integer val = (Integer) evt.getNewValue();
            m_adapterId = val;
        } else if (evt.getPropertyName().equals(
                NetworkAdapterController.ADAPTER_TYPE_ID_PROPERTY)) {
            Integer val = (Integer) evt.getNewValue();
            selectAdapterTypeCombobox(val);
        } else if (evt.getPropertyName().equals(
                NetworkAdapterController.NAME_PROPERTY)) {
            String val = (String) evt.getNewValue();
            nameTextField.setText(val);
        } else if (evt.getPropertyName().equals(
                NetworkAdapterController.TEST_PC_ID_PROPERTY)) {
            Integer val = (Integer) evt.getNewValue();
            selectTestPcCombobox(val);
        }
    }

    private String getWindowTitle() {
        if (m_editMode) {
            return new String("Edit Network Adapter");
        } else {
            return new String("Add New Network Adapter");
        }
    }

    @Override
    public boolean validateForm() {
        return true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox adapterTypeComboBox;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JComboBox pcComboBox;
    private javax.swing.JButton saveButton;
    private javax.swing.JTextField wpaDriverTextField;
    // End of variables declaration//GEN-END:variables

    private void populatePcComboBox() {
        pcComboBox.removeAllItems();
        List<TestPcRefined> li = TestPcFactory.newInstance().getTestPcList();
        for (Iterator<TestPcRefined> it = li.iterator(); it.hasNext();) {
            pcComboBox.addItem(it.next());
        }
    }

    private void selectTestPcCombobox(int testPcId) {
        DefaultComboBoxModel model = (DefaultComboBoxModel) pcComboBox.getModel();
        for (int i = 0; i < model.getSize(); i++) {
            TestPcRefined testPcRefined = (TestPcRefined) model.getElementAt(i);
            if (testPcRefined != null && testPcRefined.getId() == testPcId) {
                pcComboBox.setSelectedIndex(i);
                return;
            }
        }
    }

    private int getSelectedTestPcId() {
        int result = 0;
        TestPcRefined obj = (TestPcRefined) pcComboBox.getSelectedItem();
        if (obj != null) {
            result = obj.getId();
        }
        return result;
    }

    private void populateAdapterTypeComboBox() {
        adapterTypeComboBox.removeAllItems();
        List<NetworkAdapterTypeRefined> li = NetworkAdapterTypeFactory.newInstance().getAdapterTypeList();
        for (Iterator<NetworkAdapterTypeRefined> it = li.iterator(); it.hasNext();) {
            adapterTypeComboBox.addItem(it.next());
        }
    }

    private void selectAdapterTypeCombobox(int typeId) {
        DefaultComboBoxModel model = (DefaultComboBoxModel) adapterTypeComboBox.getModel();
        for (int i = 0; i < model.getSize(); i++) {
            NetworkAdapterTypeRefined refined = (NetworkAdapterTypeRefined) model.getElementAt(
                    i);
            if (refined != null && refined.getId() == typeId) {
                adapterTypeComboBox.setSelectedIndex(i);
                return;
            }
        }
    }

    private int getSelectedAdapterTypeId() {
        int result = 0;
        NetworkAdapterTypeRefined obj = (NetworkAdapterTypeRefined) adapterTypeComboBox.getSelectedItem();
        if (obj != null) {
            result = obj.getId();
        }
        return result;
    }
}
